package _05sql.work1;

import com.mysql.cj.jdbc.Driver;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AccountDaoImpl implements AccountDao {
    public static Connection getConnection() throws Exception {
        Connection connection = null;
        DriverManager.registerDriver(new Driver());
        String url = "jdbc:mysql://localhost:3306/db?useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8";
        String username = "root";
        String password = "123456";
        connection = DriverManager.getConnection(url, username, password);
        return connection;
    }

    public static void  close(Connection connection, Statement statement, ResultSet resultSet){
        try {
            if (resultSet != null) resultSet.close();
            if (statement!= null) statement.close();
            if (connection != null) connection.close();

        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    @Override
    public BigDecimal selectMoneyByName(String name) {
        try {
            Connection connection = getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from acount");
            List<Count> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String names = resultSet.getString("name");
                double money = resultSet.getDouble("money");
                BigDecimal bigDecimalmoney = BigDecimal.valueOf(money);
                Count count = new Count(id, names, bigDecimalmoney);
                list.add(count);
            }
            Count[] counts = list.toArray(new Count[0]);
            BigDecimal bigDecimal = null;
            for (Count count : counts) {
                if (count.getName().equals(name)) {
                    bigDecimal = count.getMoney();
                    break;
                }
            }
            close(connection,statement,resultSet);
            return bigDecimal;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean updateMoneyByName(String toName,String fromName, BigDecimal money) throws Exception {
        Connection connection=getConnection();
        double moneys=money.doubleValue();
        System.out.println(moneys);
        connection.setAutoCommit(false);
        try {
            PreparedStatement preparedStatementA=connection.prepareStatement("update acount set money=money-? where name=?");
            preparedStatementA.setDouble(1,moneys);
            preparedStatementA.setString(2,toName);
            int affectedRowsA=preparedStatementA.executeUpdate();
            System.out.println(affectedRowsA);
            if (affectedRowsA!=1){
                throw new RuntimeException("A账户信息不对"+affectedRowsA);
            }

            PreparedStatement preparedStatementB= connection.prepareStatement("update acount set money=money+? where name=?");
            preparedStatementB.setDouble(1,moneys);
            preparedStatementB.setString(2,fromName);
            int affectedRowsB=preparedStatementB.executeUpdate();
            if (affectedRowsB!=1){
                throw new RuntimeException("B账户信息不对"+affectedRowsB);
            }

            connection.commit();

        } catch (Exception e) {
            e.printStackTrace();
            connection.rollback();

            return false;
        }
        return true;

    }

    public static void main(String[] args) throws Exception {
        AccountDaoImpl accountDao = new AccountDaoImpl();
        String A="张三";
        BigDecimal bigDecimalA=accountDao.selectMoneyByName(A);
        System.out.println("A账户余额"+bigDecimalA);
        String B="李四";
        BigDecimal bigDecimalB=accountDao.selectMoneyByName(B);
        System.out.println("B账户余额"+bigDecimalB);
        double a=500;
        BigDecimal bigDecimal=BigDecimal.valueOf(a);
        if (accountDao.updateMoneyByName(A,B,bigDecimal)){
            System.out.println("A账户余额"+accountDao.selectMoneyByName(A));
            System.out.println("B账户余额"+accountDao.selectMoneyByName(B));
            System.out.println("转账成功");
        }else System.out.println("转账失败");
    }
}
class Count{
    private int id;
    private String name;
    private BigDecimal money;

    public Count(int id, String name, BigDecimal money) {
        this.id = id;
        this.name = name;
        this.money = money;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getMoney() {
        return money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Count{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
